//
//  SLIdTypeSlectVC.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/20.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import UIKit
import SwiftyJSON

class SLIdTypeSlectVC: SLTableVC<Any> {
    
    var dataArray:[JSON] = []
    var selectArr:[Bool] = []
    var handle:SLBlock?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Authentication"
        setupUI()
    }
    override func refreshData(isPullDown: Bool = true) {
        SLHttpTool.AsyncReportData(path: "/satisfies/face",params: ["sources":self.id], target: self) { responseCall, success, msg, code in
            self.tableview.mj_header?.endRefreshing()
            let data = responseCall["wavelets"]
            let arrayA = data["respective"]
            let arrayB = data["differ"]
            self.dataArray.removeAll()
            self.dataArray.append(arrayA)
            self.dataArray.append(arrayB)
            self.selectArr.append(true)
            self.selectArr.append(true)
            self.tableview.reloadData()
        }

    }
    private func setupUI() {
        view.backgroundColor = .white
        self.tableview.mj_footer = nil
        self.tableview.bounces = false
        self.tableview.register(SLTypeCell.self, forCellReuseIdentifier: "SLTypeCell")
        self.configAutoHeight(estimatedHeight: 44,autoPullDown: true,nibName: "")
        self.tableview.snp.remakeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(self.titleL.snp.bottom).offset(12)
            make.bottom.equalTo(self.nextButton.snp.top)
        }
        
    }
    lazy private var titleL:UILabel = {
        let v = create_label(text: "Select an id to verify your identity", textColor: .black, textFont: SLFontConfig.medium16, supView: self.view) { make in
            make.left.equalTo(15)
            make.top.equalTo(self.navigationView.snp.bottom).offset(12)
        }
        return v
    }()
    private lazy var nextButton:UIButton = {
        let v = create_button(title: "Next", titleColor: rgba(hex: "#FFFFFF"),titleFont: SLFontConfig.medium(size: 15),bgColor: rgba(hex: "#1608EC"), image: nil, supView: self.view, closure: { make in
            make.left.equalTo(47)
            make.right.equalTo(-47)
            make.bottom.equalTo(-12-DevConfig.safe_bottom)
            make.height.equalTo(44)
        }, selector: (target: self, action: #selector(nextClick), event:.touchUpInside))
        v.layer.masksToBounds = true
        v.layer.cornerRadius = 22
        return v
    }()
    @objc private func nextClick(){
     
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        return dataArray.count
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let array = dataArray[section].arrayValue
        let isSelect = selectArr[section]
        if isSelect{
            return array.count
        }
        return 0
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let array = dataArray[indexPath.section].arrayValue
        let type = array[indexPath.row].stringValue
        let cell = tableView.dequeueReusableCell(withIdentifier: "SLTypeCell", for: indexPath) as! SLTypeCell
        
        cell.titleL.text = type
        if indexPath.row == 0 {
            cell.config(type: 0)
        }else if indexPath.row == array.count-1{
            cell.config(type: 2)
        }else{
            cell.config(type: 1)
        }
        cell.selectionStyle = .none
        return cell
    }
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let v = sectionView()
        v.arrow.isHidden = false
        if section == 0 {
            v.titleL.text = "Recommended ID Type"
        }else{
            v.titleL.text = "Other Options"
        }
        let isSelect = selectArr[section]
        v.configSelect(isSelect: isSelect)
        v.handle = { [weak self] object in
            if let ss = self{
                ss.selectArr[section] = !ss.selectArr[section]
                ss.tableview.reloadData()
            }
        }
        return v
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 64
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let array = dataArray[indexPath.section].arrayValue
        let type = array[indexPath.row].stringValue
        
        if let block = self.handle{
            self.navigationController?.popViewController(animated: true)
            block(type)
            
        }
    }
    class func showAlert(page:UIViewController,id: String,handle: SLBlock?){
        let picker = SLIdTypeSlectVC()
        picker.id = id
        picker.handle = handle
        page.navigationController?.pushViewController(picker, animated: true)
    }
    
}
class sectionView:UIView {
    var handle:SLBlock?
    
    private lazy var backView:UIView = {
        let v = UIView()
        v.backgroundColor = rgba(hex: "#1608EC")
        v.layer.cornerRadius = 10
        v.layer.masksToBounds = true
        self.addSubview(v)
        v.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(10)
            make.bottom.equalTo(-10)
        }
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapClick))
        v.addGestureRecognizer(tap)
        return v
    }()
    @objc private func tapClick(){
        if let block = self.handle{
            block(true)
        }
    }
    lazy var titleL:UILabel = {
        let v = create_label(textColor: .white, textFont: SLFontConfig.medium(size: 16), supView: self.backView) { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(16)
        }
        return v
    }()
    func configSelect(isSelect:Bool){
        self.arrow.image = isSelect ? IMAGE("arrow_u"):IMAGE("arrow_d")
    }
    
    lazy var arrow:UIImageView = {
        let v = create_imageView(image: IMAGE("arrow_d"), mode: .scaleToFill, supView: self.backView) { make in
            make.width.height.equalTo(17)
            make.right.equalTo(-15)
            make.centerY.equalToSuperview()
        }
        return v
    }()
}
class SLTypeCell:UITableViewCell {
    
    private lazy var backView:UIView = {
        let v = UIView()
        v.backgroundColor = rgba(hex: "#F3F6F9")
        v.layer.masksToBounds = true
        self.contentView.addSubview(v)
        self.contentView.sendSubviewToBack(v)
        v.snp.makeConstraints { make in
            make.left.right.top.bottom.equalTo(0)
        }
        return v
    }()
    //0 topleft topright 1 none  2 bottomleft,bottomright
    func config(type: Int){
        let frame = CGRect(x: 0, y: 0, width: DevConfig.width-30, height: 45)
        DispatchQueue.main.async {
            if type == 0 {
                //这里设置的是左上和左下角
                let maskPath:UIBezierPath = UIBezierPath.init(roundedRect:frame, byRoundingCorners: [.topLeft,.topRight], cornerRadii: CGSizeMake(10, 10))
                let maskLayer = CAShapeLayer()
                maskLayer.frame = frame
                maskLayer.path = maskPath.cgPath
                self.backView.layer.mask = maskLayer
            }else if type == 2{
                let maskPath:UIBezierPath = UIBezierPath.init(roundedRect: frame, byRoundingCorners: [.bottomLeft,.bottomRight], cornerRadii: CGSizeMake(10, 10))
                let maskLayer = CAShapeLayer()
                maskLayer.frame = frame
                maskLayer.path = maskPath.cgPath
                self.backView.layer.mask = maskLayer
            }else{
                let maskPath:UIBezierPath = UIBezierPath.init(roundedRect: frame, cornerRadius: 0)
                let maskLayer = CAShapeLayer()
                maskLayer.frame = frame
                maskLayer.path = maskPath.cgPath
                self.backView.layer.mask = maskLayer
            }
            self.setNeedsDisplay()
            
        }
        
    }
    lazy var titleL:UILabel = {
        let v = create_label(textColor: .black, textFont: SLFontConfig.regular15, supView: self.contentView) { make in
            make.centerY.equalTo(self.backView.snp.centerY)
            make.left.equalTo(16)
            
        }
        return v
    }()
    lazy var arrow:UIImageView = {
        let v = create_imageView(image: IMAGE("arrow_b"), mode: .scaleToFill, supView: self.contentView) { make in
            make.width.height.equalTo(17)
            make.right.equalTo(-15)
            make.centerY.equalToSuperview()
        }
        return v
    }()
}
